package com.wzcl.app.controller;

import com.wzcl.app.dao.model.EssayCourseQuestionBank;
import com.wzcl.app.dao.model.UserTaskQuizAnswer;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.service.UserTaskQuizAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Iterator;
import java.util.List;

/**
 * @author beingven
 * @Description TODO
 * @createTime 2020年12月28日 14:41:00
 */
@RestController
@RequestMapping("/quizAnswer")
@Api(value = "用户答题controller", tags = {"用户答题相关接口"})
public class UserTaskQuizAnswerController {
    @Resource
    private UserTaskQuizAnswerService userTaskQuizAnswerService;

    @GetMapping("/getStar/{taskId}")
    @ApiOperation("获取用户评分")
    public RVO getStar(@PathVariable Integer taskId) {
        return RVO.success(userTaskQuizAnswerService.getStar(taskId));
    }

    @PostMapping("/create")
    @ApiOperation("保存用户答题记录")
    public RVO saveAnswer(@RequestBody @Valid List<UserTaskQuizAnswer> userTaskQuizAnswers) {
        System.out.println("--------------------------------近来第一次-----------------------------------");
        System.out.println("用户答题完毕："+userTaskQuizAnswers.size()+"道");
        Iterator<UserTaskQuizAnswer> iterator = userTaskQuizAnswers.iterator();
        while (iterator.hasNext()) {
            System.out.println("用户回答的题"+iterator.next());
        }
        System.out.println("----------------------------------------------------------------------------");
        return userTaskQuizAnswerService.saveAnswer(userTaskQuizAnswers);
    }

    @GetMapping("/retrieve/{taskId}")
    @ApiOperation("查询当前用户在任务中的答题进度")
    public RVO retrieveUserAnswerQuestion(@PathVariable Integer taskId) {
        EssayCourseQuestionBank questionBank = userTaskQuizAnswerService.retrieveUserAnswerQuestion(taskId);
        if (questionBank == null){
            return RVO.success("该用户未开始答题");
        }
        return RVO.success("返回当前进度", questionBank);
    }

    @DeleteMapping("/resetTask/{taskId}")
    @ApiOperation("重置任务")
    public RVO resetTask(@PathVariable Integer taskId) {
        return userTaskQuizAnswerService.resetTask(taskId);
    }
}